/* $Id$ */
/*
 * tup-provider.c
 * Copyright (C)  2011 Flos Lonicerae <lonicerae@gmail.com>
 *
 * tup-provider.c is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */
/*
   This software is based in part on the work of thunar-vcs-plugin written
   by Peter de Ridder <peter@xfce.org> and thunar-archive-plugin written by
   Benedikt Meurer <benny@xfce.org>
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif

#include <thunar-vfs/thunar-vfs.h>

#include <thunar-tortoisehg-plugin/tup-provider.h>
#include "ttp-hg-action.h"

/* use g_access() on win32 */
#if defined(G_OS_WIN32)
#include <glib/gstdio.h>
#else
#define g_access(filename, mode) access((filename), (mode))
#endif


static void   tup_provider_class_init           (TupProviderClass         *klass);

static void   tup_provider_init                 (TupProvider              *tup_provider);

static void   tup_provider_finalize             (GObject                  *object);


static void   tup_provider_menu_provider_init   (ThunarxMenuProviderIface *iface);

static GList *tup_provider_get_file_actions     (ThunarxMenuProvider      *menu_provider,
                                                 GtkWidget                *window,
                                                 GList                    *files);

static GList *tup_provider_get_folder_actions (ThunarxMenuProvider	*menu_provider,
					       GtkWidget		*window,
					       ThunarxFileInfo	*folder);




struct _TupProviderClass
{
	GObjectClass __parent__;
};


struct _TupProvider
{
	GObject         __parent__;

#if !GTK_CHECK_VERSION(2,9,0)
	/*
	GTK+ 2.9.0 and above provide an icon-name property
	for GtkActions, so we don't need the icon factory.
	*/
	GtkIconFactory *icon_factory;
#endif

	/*
	child watch support for the last spawn command,
	which allows us to refresh the folder contents
	after the command terminates (i.e. the archive
	is created).
	*/
	gchar          *child_watch_path;
	gint            child_watch_id;
};


static GList*
tup_provider_get_pages (ThunarxPropertyPageProvider *page_provider, GList *files)
{
	GList *pages = NULL;
	return pages;
}


static void
tup_provider_property_page_provider_init (ThunarxPropertyPageProviderIface *iface)
{
	iface->get_pages = tup_provider_get_pages;
}



THUNARX_DEFINE_TYPE_WITH_CODE (TupProvider,
			       tup_provider,
			       G_TYPE_OBJECT,
			       THUNARX_IMPLEMENT_INTERFACE (THUNARX_TYPE_MENU_PROVIDER,
							    tup_provider_menu_provider_init)
			       THUNARX_IMPLEMENT_INTERFACE (THUNARX_TYPE_PROPERTY_PAGE_PROVIDER,
							    tup_provider_property_page_provider_init));

static void
tup_provider_class_init (TupProviderClass *klass)
{
	GObjectClass *gobject_class;

	gobject_class = G_OBJECT_CLASS (klass);
	gobject_class->finalize = tup_provider_finalize;
}


/* define different actions list for menu-provider-iface */
static void
tup_provider_menu_provider_init (ThunarxMenuProviderIface *iface)
{
	/* actions for thunarx right files part */
	iface->get_file_actions = tup_provider_get_file_actions;
	/* actions for thunarx left folder tree part */
	iface->get_folder_actions = tup_provider_get_folder_actions;
}


static void
tup_provider_init (TupProvider *tup_provider)
{
#if !GTK_CHECK_VERSION(2,9,0)
	GtkIconSource *icon_source;
	GtkIconSet    *icon_set;

	/* setup our icon factory */
	tup_provider->icon_factory = gtk_icon_factory_new ();
	gtk_icon_factory_add_default (tup_provider->icon_factory);

	/* add the "mercurial" stock icon */
	icon_set = gtk_icon_set_new ();
	icon_source = gtk_icon_source_new ();
	gtk_icon_source_set_icon_name (icon_source, "mercurial");
	gtk_icon_set_add_source (icon_set, icon_source);
	gtk_icon_factory_add (tup_provider->icon_factory, "mercurial", icon_set);
	gtk_icon_source_free (icon_source);
	gtk_icon_set_unref (icon_set);
#endif /* !GTK_CHECK_VERSION(2,9,0) */
}


/* FIXME */
static void
tup_provider_finalize (GObject *object)
{
	TupProvider *tup_provider = TUP_PROVIDER (object);
	GSource     *source;

	/* give up maintaince of any pending child watch */
	//  if (G_UNLIKELY (tup_provider->child_watch_id != 0))
	//    {
	//      /* reset the callback function to g_spawn_close_pid() so the plugin can be
	//       * safely unloaded and the child will still not become a zombie afterwards.
	//       * This also resets the child_watch_id and child_watch_path properties.
	//       */
	//      source = g_main_context_find_source_by_id (NULL, tup_provider->child_watch_id);
	//      g_source_set_callback (source, (GSourceFunc) g_spawn_close_pid, NULL, NULL);
	//    }

#if !GTK_CHECK_VERSION(2,9,0)
	/* release our icon factory */
	gtk_icon_factory_remove_default (tup_provider->icon_factory);
	g_object_unref (G_OBJECT (tup_provider->icon_factory));
#endif

	(*G_OBJECT_CLASS (tup_provider_parent_class)->finalize) (object);
}


//static gboolean
//tup_is_parent_writable (ThunarxFileInfo *file_info)
//{
//	gboolean result = FALSE;
//	gchar   *filename;
//	gchar   *uri;

//	/* determine the parent URI for the file info */
//	uri = thunarx_file_info_get_parent_uri (file_info);
//	if (G_LIKELY (uri != NULL))
//	{
//		/* determine the local filename for the URI */
//		filename = g_filename_from_uri (uri, NULL, NULL);
//		if (G_LIKELY (filename != NULL))
//		{
//			/* check if we can write to that folder */
//			result = (g_access (filename, W_OK) == 0);

//			/* release the filename */
//			g_free (filename);
//		}

//		/* release the URI */
//		g_free (uri);
//	}

//	return result;
//}


static GList*
tup_provider_get_file_actions (ThunarxMenuProvider *menu_provider,
                               GtkWidget           *window,
                               GList               *files)
{
	ThunarVfsPathScheme scheme;
	ThunarVfsInfo      *info;
	//TupProvider        *tup_provider = TUP_PROVIDER (menu_provider);
	GtkAction          *action;
	GList              *actions = NULL;
	GList              *lp;
	gint                n_files = 0;

//	GClosure           *closure;
//	gboolean            all_archives = TRUE;
//	gboolean            can_write = TRUE;

	gboolean is_parent = FALSE;
	gboolean is_directory = FALSE;
	gboolean is_file = FALSE;

	/* check all supplied files */
	for (lp = files; lp != NULL; lp = lp->next, ++n_files)
	{
		/* check if the file is a local file */
		info = thunarx_file_info_get_vfs_info (lp->data);
		scheme = thunar_vfs_path_get_scheme (info->path);
		thunar_vfs_info_unref (info);

		/* unable to handle non-local files */
		if (G_UNLIKELY (scheme != THUNAR_VFS_PATH_SCHEME_FILE)) {
			return NULL;
		}

		/* FIXME: more than one file or directory should be considered */
		if(thunarx_file_info_is_directory(lp->data)) {
			is_directory = TRUE;
		}
		else {
			is_file = TRUE;
		}

		/* check if we can write to the parent folder */
		//      if (can_write && !tup_is_parent_writable (lp->data))
		//        can_write = FALSE;
	}

	/* FIXME */
	action = ttp_hg_action_new("Tup::ttp", "TortoiseHg", files, window, is_parent, is_directory, is_file);
	actions = g_list_append(actions, action);

	return actions;
}


static GList*
tup_provider_get_folder_actions(ThunarxMenuProvider *menu_provider,
				GtkWidget *window,
				ThunarxFileInfo *folder)
{
	GtkAction *action;
	GList *actions = NULL;
	ThunarVfsPathScheme scheme;
	ThunarVfsInfo *info;
	GList *files;

	g_message("get_folder_actions()");

	/* check if the file is a local file */
	info = thunarx_file_info_get_vfs_info(folder);
	scheme = thunar_vfs_path_get_scheme(info->path);
	thunar_vfs_info_unref(info);

	/* unable to handle non-local files,
	   check path scheme if the action is appropriate */
	if(G_UNLIKELY(scheme != THUNAR_VFS_PATH_SCHEME_FILE))
	{
		return NULL;
	}

	files = g_list_append(NULL, folder);

	action = ttp_hg_action_new("Tup::ttp", "TortoiseHg", files, window, TRUE, TRUE, FALSE);
	/* append the tortoisehg submenu action */
	actions = g_list_append(actions, action);

	g_list_free(files);
	return actions;
}

